home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / SQLite.au3 < prev    next >
Encoding:
Text File  |  2007-09-08  |  50.2 KB  |  1,161 lines

  1. ; Include Version:1.78 (07.08.07)
  2. #include-once
  3.  
  4. ; ------------------------------------------------------------------------------
  5. ;
  6. ; AutoIt Version: 3.2.4.9
  7. ; Language:       English
  8. ; Description:    Functions that assist access to an SQLite database.
  9. ;
  10. ; ------------------------------------------------------------------------------
  11. ; This software is provided 'as-is', without any express or
  12. ; implied warranty.  In no event will the authors be held liable for any
  13. ; damages arising from the use of this software.
  14.  
  15. ; function list
  16. ;===============================================================================
  17. ; _SQLite_Startup
  18. ; _SQLite_Shutdown
  19. ; _SQLite_Open
  20. ; _SQLite_Close
  21. ; _SQLite_GetTable
  22. ; _SQLite_Exec
  23. ; _SQLite_LibVersion
  24. ; _SQLite_LastInsertRowID
  25. ; _SQLite_GetTable2d
  26. ; _SQLite_Changes
  27. ; _SQLite_TotalChanges
  28. ; _SQLite_ErrCode
  29. ; _SQLite_ErrMsg
  30. ; _SQLite_Display2DResult
  31. ; _SQLite_FetchData
  32. ; _SQLite_Query
  33. ; _SQLite_SetTimeout
  34. ; _SQLite_SaveMode
  35. ; _SQLite_QueryFinalize
  36. ; _SQLite_QueryReset
  37. ; _SQLite_FetchNames
  38. ; _SQLite_QuerySingleRow
  39. ; _SQLite_SQLiteExe
  40. ; _SQLite_Encode
  41. ; _SQLite_Escape
  42.  
  43. #comments-start
  44.     User Calltips:
  45.     _SQLite_Startup([$sDll_Filename]) Loads SQLite3.dll
  46.     _SQLite_Shutdown() Unloads SQLite3.dll
  47.     _SQLite_Open([$sDatabase_Filename]) Opens Database, Sets Standard Handle, Returns Handle
  48.     _SQLite_Close([$hDB]) Closes Database
  49.     _SQLite_GetTable($hDB | -1 , $sSQL , ByRef $aResult , ByRef $iRows , ByRef $iColumns , [$iCharSize]) Executes $sSQL Query to $aResult, Returns Error Code
  50.     _SQLite_Exec($hDB | -1 , $sSQL, [$sCallback = ""]) Executes $sSQL , Returns Error Code
  51.     _SQLite_LibVersion() Returns Dll's Version No.
  52.     _SQLite_LastInsertRowID($hDB) Returns Last INSERT ROWID
  53.     _SQLite_GetTable2d($hDB | -1 , $sSQL , ByRef $aResult , ByRef $iRows , ByRef $iColumns , [$iCharSize], [$fSwichDimensions = False]) Executes $sSQL Query to $aResult, Returns Error Code
  54.     _SQLite_Changes([$hDB]) Returns Number of Changes (Excluding Triggers) of The last Transaction
  55.     _SQLite_TotalChanges([$hDB]) Returns Number of All Changes (Including Triggers) of all Transactions
  56.     _SQLite_ErrCode([$hDB]) Returns Last Error Code (Numeric)
  57.     _SQLite_ErrMsg([$hDB]) Returns Last Error Message
  58.     _SQLite_Display2DResult($aResult , [$iCellWidth = 0], [$fReturn = False]) Returns or Prints a 2d Array to console
  59.     _SQLite_FetchData($hQuery, ByRef $aRow, [$fBinary = False] ) Fetches Results From First/Next Row of $hQuery Query into $aRow, Returns Error Code
  60.     _SQLite_Query($hDB | -1 , $sSQL , ByRef $hQuery) Prepares $sSql, Returns Error Code
  61.     _SQLite_SetTimeout([$hDB = -1] , [$iTimeout = 1000]) Sets Timeout for busy handler
  62.     _SQLite_SaveMode($fSaveModeState) Turn Savemode On or Off (boolean)
  63.     _SQLite_QueryFinalize($hQuery) Finalizes a Query
  64.     _SQLite_QueryReset($hQuery) Resets a Query
  65.     _SQLite_FetchNames($hQuery, ByRef $aNames) Read out the Tablenames of a _SQLite_Query() based query
  66.     _SQLite_QuerySingleRow($hDB | -1 , $sSQL , ByRef $aRow) Read out the first Row of the Result from the Specified query
  67.     _SQLite_SQLiteExe( $sDatabaseFile , $sInput , ByRef $sOutput , [$sSQLiteExeFilename = "SQLite3.exe"] ) Executes commands in SQLite.exe
  68.     _SQLite_Encode($vData) Returns Encoded String
  69.     _SQLite_Escape($sString,[$iBuffSize]) Retruns Escaped String
  70.     
  71.     Changelog:
  72.     26.11.05    Added _SQLite_QueryReset()
  73.     26.11.05    Added _SQLite_QueryFinalize()
  74.     26.11.05     Added _SQLite_SaveMode()
  75.     26.11.05     Implemented SaveMode
  76.     27.11.05    Renamed _SQLite_FetchArray() -> _SQLite_FetchData()
  77.     27.11.05    Added _SQLite_FetchNames(), Example
  78.     28.11.05    Removed _SQLite_Commit(), _SQLite_Close() handles $SQLITE_BUSY issues
  79.     28.11.05    Added Function Headers
  80.     28.11.05    Fixed Bug in _SQLite_Exec(), $sErrorMsg was set to 0 instead of 'Successful result'
  81.     29.11.05    Changed _SQLite_Display2DResult(), Better Fromating for Lager Tables & Ability to Return the Result
  82.     30.11.05    Changed _SQLite_GetTable2d(), Ability to Swich Dimensions
  83.     30.11.05    Fixed _SQLite_Display2DResult() $iCellWidth was ignored
  84.     03.12.05    Added _SQLite_QuerySingleRow()
  85.     04.12.05    Changed Standard $hDB Handling (Thank you JPM)
  86.     04.12.05    Fixed Return Values of _SQLite_LibVersion(),_SQLite_LastInsertRowID(),_SQLite_Changes(),_SQLite_TotalChanges()
  87.     04.12.05    Changed _SQLite_Open() now opens a ':memory:' database if no name specified
  88.     05.12.05    Changed _SQLite_FetchData() NULL Values will be Skipped
  89.     10.12.05    Changed _SQLite_QuerySingleResult() now uses 'sqlite3_get_table' API
  90.     13.12.05    Added _SQLite_SQLiteExe() Wrapper for SQLite3.exe
  91.     29.03.06    Removed _SQLite_SetGlobalTimeout()
  92.     29.03.06    Added _SQLite_SetTimeout()
  93.     17.05.06    :cdecl to support autoit debugging version
  94.     18.05.06    _SQLite_SQLiteExe() now Creates nonexistend Directory's
  95.     18.05.06    Fixed SyntaxCheck Warnings (_SQLite_GetTable2d())
  96.     21.05.06    Added support for Default Keyword for all Optional parameters
  97.     25.05.06    Added _SQLite_Encode()
  98.     25.05.06    Changed _SQLite_QueryNoResult() -> _SQLite_Execute()
  99.     25.05.06    Changed _SQLite_FetchData() Binary Mode
  100.     26.05.06    Removed _SQLite_GlobalRecover() out-of-memory recovery is automatic since SQLite 3.3.0
  101.     26.05.06    Changed @error Values & Improved error catching (see Function headers)
  102.     31.05.06    JPM's Nice @error values setting
  103.     04.06.06    Inline SQLite3.dll
  104.     08.06.06    Changed _SQLite_Exec(), _SQLite_GetTable2d(), _SQLite_GetTable() Removed '$sErrorMsg' parameter
  105.     08.06.06    Removed _SQLite_Execute() because _SQLite_Exec() was the same
  106.     08.06.06    Cleaning _SQlite_Startup(). (JPM)
  107.     23.09.06    Fixed _SQLite_Exec() Memory Leak on SQL error
  108.     23.09.06    Added SQL Error Reporting (only in interpreted mode)
  109.     23.09.06    Added _SQLite_Escape()
  110.     24.09.06    Changed _SQLite_Escape(), Changed _SQLite_GetTable*() New szString Reading method, Result will no longer be truncated
  111.     25.09.06    Fixed Bug in szSring read procedure (_SQLite_GetTable*, _SQLite_QuerySingleRow, _SQLite_Escape)
  112.     29.09.06    Faster szString Reading, Function Header corrections
  113.     29.09.06    Changed _SQLite_Exec() Callback
  114.     12.03.07    Changed _SQLite_Query() to use 'sqlite3_prepare_v2' API
  115.     16.03.07    Fixed _SQLite_Open() not setting @error, Missing DllClose() in _SQLite_Shutdown(), Stack corruption in szString reading procedure
  116.     17.03.07    Improved Error handling/Reporting
  117.     08.07.07    Fixed Bug in version comparison procedure
  118. #comments-end
  119.  
  120. Global Const $SQLITE_OK = 0   ; /* Successful result */
  121. Global Const $SQLITE_ERROR = 1   ; /* SQL error or missing database */
  122. Global Const $SQLITE_INTERNAL = 2   ; /* An internal logic error in SQLite */
  123. Global Const $SQLITE_PERM = 3   ; /* Access permission denied */
  124. Global Const $SQLITE_ABORT = 4   ; /* Callback routine requested an abort */
  125. Global Const $SQLITE_BUSY = 5   ; /* The database file is locked */
  126. Global Const $SQLITE_LOCKED = 6   ; /* A table in the database is locked */
  127. Global Const $SQLITE_NOMEM = 7   ; /* A malloc() failed */
  128. Global Const $SQLITE_READONLY = 8   ; /* Attempt to write a readonly database */
  129. Global Const $SQLITE_INTERRUPT = 9   ; /* Operation terminated by sqlite_interrupt() */
  130. Global Const $SQLITE_IOERR = 10   ; /* Some kind of disk I/O error occurred */
  131. Global Const $SQLITE_CORRUPT = 11   ; /* The database disk image is malformed */
  132. Global Const $SQLITE_NOTFOUND = 12   ; /* (Internal Only) Table or record not found */
  133. Global Const $SQLITE_FULL = 13   ; /* Insertion failed because database is full */
  134. Global Const $SQLITE_CANTOPEN = 14   ; /* Unable to open the database file */
  135. Global Const $SQLITE_PROTOCOL = 15   ; /* Database lock protocol error */
  136. Global Const $SQLITE_EMPTY = 16   ; /* (Internal Only) Database table is empty */
  137. Global Const $SQLITE_SCHEMA = 17   ; /* The database schema changed */
  138. Global Const $SQLITE_TOOBIG = 18   ; /* Too much data for one row of a table */
  139. Global Const $SQLITE_CONSTRAINT = 19   ; /* Abort due to constraint violation */
  140. Global Const $SQLITE_MISMATCH = 20   ; /* Data type mismatch */
  141. Global Const $SQLITE_MISUSE = 21   ; /* Library used incorrectly */
  142. Global Const $SQLITE_NOLFS = 22   ; /* Uses OS features not supported on host */
  143. Global Const $SQLITE_AUTH = 23   ; /* Authorization denied */
  144. Global Const $SQLITE_ROW = 100   ; /* sqlite_step() has another row ready */
  145. Global Const $SQLITE_DONE = 101   ; /* sqlite_step() has finished executing */
  146.  
  147. Global Const $SQLITE_DBHANDLE = 1  ; /* (Internal Only) Database Handle (sqlite3*) */
  148. Global Const $SQLITE_QUERYHANDLE = 2  ; /* (Internal Only) Query Handle (sqlite3_stmt*) */
  149.  
  150. #include <Array.au3>     ; Using: _ArrayCreate(),_ArrayAdd(),_ArrayDelete(),_ArraySearch()
  151. #include <File.au3>     ; Using: _TempFile()
  152.  
  153. Global $g_hDll_SQLite = 0
  154. Global $g_hDB_SQLite = 0
  155. Global $g_avSafeMode_SQLite = _ArrayCreate(1, _ArrayCreate(''), _ArrayCreate(''), 0, _ArrayCreate(''))
  156.  
  157. ;===============================================================================
  158. ;
  159. ; Function Name:    _SQLite_Startup
  160. ;
  161. ; Parameter(s):        $sDll_Filename - Optional, Dll Filename
  162. ; Description:      Loads SQLite.dll
  163. ; Requirement:        None
  164. ; Return Value(s):  On Success - Returns path to SQLite3.dll
  165. ;                   On Failure - Returns empty string
  166. ; @error Value(s):    1 - Error Loading Dll
  167. ;
  168. ; User CallTip:        _SQLite_Startup([$sDll_Filename]) Loads SQLite3.dll
  169. ; Author(s):        piccaso (Fida Florian), JPM
  170. ;
  171. ;===============================================================================
  172.  
  173. Func _SQLite_Startup($sDll_Filename = "") ; Loads SQLite Dll
  174.     Local $hDll, $hFileDllOut = -1
  175.     Local $fUseInline = True
  176.     Local $vInlineVersion = Call('__' & 'SQLite_Inline_Version')
  177.     If @error Then $fUseInline = False
  178.     If IsKeyword($sDll_Filename) Or $sDll_Filename = "" Or $sDll_Filename = -1 Then
  179.         $sDll_Filename = "sqlite3.dll"
  180.         If __SQLite_VersCmp(@ScriptDir & "\" & $sDll_Filename, $vInlineVersion) = $SQLITE_OK Then
  181.             $sDll_Filename = @ScriptDir & "\" & $sDll_Filename
  182.             $fUseInline = False
  183.         ElseIf __SQLite_VersCmp(@SystemDir & "\" & $sDll_Filename, $vInlineVersion) = $SQLITE_OK Then
  184.             $sDll_Filename = @SystemDir & "\" & $sDll_Filename
  185.             $fUseInline = False
  186.         ElseIf __SQLite_VersCmp(@WindowsDir & "\" & $sDll_Filename, $vInlineVersion) = $SQLITE_OK Then
  187.             $sDll_Filename = @WindowsDir & "\" & $sDll_Filename
  188.             $fUseInline = False
  189.         ElseIf __SQLite_VersCmp(@WorkingDir & "\" & $sDll_Filename, $vInlineVersion) = $SQLITE_OK Then
  190.             $sDll_Filename = @WorkingDir & "\" & $sDll_Filename
  191.             $fUseInline = False
  192.         EndIf
  193.         If $fUseInline Then
  194.             $sDll_Filename = @SystemDir & "\SQLite3.dll"
  195.             If Not FileExists($sDll_Filename) Then
  196.                 $hFileDllOut = FileOpen($sDll_Filename, 2)
  197.             EndIf
  198.             If $hFileDllOut = -1 Then
  199.                 $sDll_Filename = _TempFile(@TempDir, "~", ".dll")
  200.                 $hFileDllOut = FileOpen($sDll_Filename, 2)
  201.                 If $hFileDllOut = -1 Then Return SetError(1, 0, "")
  202.                 _ArrayAdd($g_avSafeMode_SQLite[4], $sDll_Filename)
  203.             EndIf
  204.             FileWrite($hFileDllOut, Call('__' & 'SQLite_Inline_SQLite3Dll'))
  205.             FileClose($hFileDllOut)
  206.             FileSetTime($sDll_Filename, Call('__' & 'SQLite_Inline_Modified'), 0)
  207.         EndIf
  208.     EndIf
  209.     $hDll = DllOpen($sDll_Filename)
  210.     If $hDll = -1 Then
  211.         Return SetError(1, 0, "")
  212.     Else
  213.         $g_hDll_SQLite = $hDll
  214.         Return $sDll_Filename
  215.     EndIf
  216. EndFunc   ;==>_SQLite_Startup
  217.  
  218. ;===============================================================================
  219. ;
  220. ; Function Name:    _SQLite_Shutdown()
  221. ; Description:      Unloads SQLite Dll
  222. ; Return Value(s):  None
  223. ; Author(s):        piccaso (Fida Florian)
  224. ;
  225. ;===============================================================================
  226. ;
  227. Func _SQLite_Shutdown() ; Unloads SQLite Dll
  228.     If $g_hDll_SQLite > 0 Then DllClose($g_hDll_SQLite)
  229.     $g_hDll_SQLite = 0
  230.     If $g_avSafeMode_SQLite[3] > 0 Then DllClose($g_avSafeMode_SQLite[3])
  231.     $g_avSafeMode_SQLite[3] = 0
  232.     For $sTempFile In $g_avSafeMode_SQLite[4]
  233.         If FileExists($sTempFile) Then FileDelete($sTempFile)
  234.     Next
  235. EndFunc   ;==>_SQLite_Shutdown
  236.  
  237. ;===============================================================================
  238. ;
  239. ; Function Name:      _SQLite_Open()
  240. ; Description:        Opens a Database
  241. ; Parameter(s):       $sDatabase_Filename - Optional, Database Filename (uses ':memory:' db by default)
  242. ; Return Value(s):    Returns Database Handle
  243. ; @error Value(s):       1 - Error Calling SQLite API 'sqlite3_open'
  244. ;                      -1 - SQLite Reported an Error (Check @extended Value)
  245. ; @extended Value(s): Can be compared against $SQLITE_* Constants
  246. ; Author(s):          piccaso (Fida Florian)
  247. ;
  248. ;===============================================================================
  249. ;
  250. Func _SQLite_Open($sDatabase_Filename = ":memory:")
  251.     Local $avRval
  252.     If IsKeyword($sDatabase_Filename) Then $sDatabase_Filename = ":memory:"
  253.     $avRval = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_open", "str", $sDatabase_Filename _ ; Database filename
  254.             , "long_ptr", 0) ; OUT: SQLite db handle
  255.     If @error > 0 Then
  256.         Return SetError(1, $SQLITE_MISUSE, 0)
  257.     ElseIf $avRval[0] = $SQLITE_OK Then
  258.         $g_hDB_SQLite = $avRval[2]
  259.         __SQLite_hAdd($avRval[2], $SQLITE_DBHANDLE)
  260.         Return SetError(0, $avRval[0], $avRval[2])
  261.     Else
  262.         __SQLite_ReportError($avRval[2], "_SQLite_Open")
  263.         _SQLite_Close($avRval[2])
  264.         Return SetError(-1, $avRval[0], 0)
  265.     EndIf
  266. EndFunc   ;==>_SQLite_Open
  267. ;===============================================================================
  268. ;
  269. ; Function Name:    _SQLite_GetTable()
  270. ; Description:      Passes Out a 1Dimensional Array Containing Tablenames and Data of Executed Query
  271. ; Parameter(s):     $hDB - An Open Database, Use -1 To use Last Opened Database
  272. ;                    $sSQL - SQL Statement to be executed
  273. ;                    ByRef $aResult - Passes out the Result
  274. ;                    ByRef $iRows - Passes out the amount of 'data' Rows
  275. ;                    ByRef $iColumns - Passes out the amount of Columns
  276. ;                    $iCharSize - Optional, Specifies the maximal size of a Data Field
  277. ; Return Value(s):  On Success - Returns $SQLITE_OK
  278. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  279. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  280. ;                    1 - Error Calling SQLite API 'sqlite3_get_table'
  281. ;                     2 - Error Calling SQLite API 'sqlite3_free_table'
  282. ;                     3 - Call Prevented by SaveMode
  283. ; Author(s):        piccaso (Fida Florian)
  284. ;
  285. ;===============================================================================
  286. ;
  287. Func _SQLite_GetTable($hDB, $sSQL, ByRef $aResult, ByRef $iRows, ByRef $iColumns, $iCharSize = -1)
  288.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(3, 0, $SQLITE_MISUSE)
  289.     Local $r, $iResultSize, $i, $struct1, $struct2, $pResult
  290.     If $iCharSize = 0 Or $iCharSize = "" Or $iCharSize < 1 Or IsKeyword($iCharSize) Then $iCharSize = -1
  291.     $r = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_get_table", _
  292.             "ptr", $hDB, _ ; An open database
  293.             "str", $sSQL, _; SQL to be executed
  294.             "long_ptr", 0, _ ; Result written to a char *[]  that this points to
  295.             "long_ptr", 0, _ ; Number of result rows written here
  296.             "long_ptr", 0, _ ; Number of result columns written here
  297.             "long_ptr", 0)  ; Error msg written here
  298.     If @error > 0 Then
  299.         Return SetError(1, 0, $SQLITE_MISUSE) ; Dll Calling Error
  300.     EndIf
  301.     $pResult = $r[3]
  302.     $iRows = $r[4]
  303.     $iColumns = $r[5]
  304.     $iResultSize = (($iRows) + 1) * ($iColumns)
  305.     For $i = 1 To $iResultSize - 1
  306.         $struct1 &= "ptr;"
  307.     Next
  308.     $struct1 &= "ptr"
  309.     $struct2 = DllStructCreate($struct1, $pResult)
  310.     If Not IsArray($aResult) Then
  311.         Dim $aResult[1]
  312.     EndIf
  313.     ReDim $aResult [$iResultSize + 1]
  314.     $aResult[0] = $iResultSize
  315.     For $i = 1 To $iResultSize
  316.         ;$aResult[$i] = DllStructGetData(DllStructCreate("char[" & $iCharSize & "]", DllStructGetData($struct2, $i)), 1)
  317.         $aResult[$i] = __SQLite_szStringRead(DllStructGetData($struct2, $i), $iCharSize)
  318.     Next
  319.     DllCall($g_hDll_SQLite, "none:cdecl", "sqlite3_free_table", "ptr", $pResult) ; pointer to 'resultp' from sqlite3_get_table
  320.     If @error > 0 Then SetError(2)
  321.     If $r[0] <> $SQLITE_OK Then
  322.         __SQLite_ReportError($hDB, "_SQLite_GetTable", $sSQL)
  323.         SetError(-1)
  324.     EndIf
  325.     Return $r[0]
  326. EndFunc   ;==>_SQLite_GetTable
  327.  
  328. ;===============================================================================
  329. ;
  330. ; Function Name:    _SQLite_Exec()
  331. ; Description:      Executes a SQLite Query, Does not handle Results
  332. ; Parameter(s):     $hDB - An Open Database, Use -1 To use Last Opened Database
  333. ;                    $sSQL - SQL Statement to be executed
  334. ;                    $sCallBack - Optional, Callback Function
  335. ; Return Value(s):  On Success - Returns $SQLITE_OK
  336. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  337. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  338. ;                     1 - Error Calling SQLite API 'sqlite3_exec'
  339. ;                     2 - Call Prevented by SaveMode
  340. ;                     3 - Error Processing Callback
  341. ; Author(s):        piccaso (Fida Florian)
  342. ;
  343. ;===============================================================================
  344. ;
  345. Func _SQLite_Exec($hDB, $sSQL, $sCallBack = "")
  346.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(2, 0, $SQLITE_MISUSE)
  347.     Local $avRval, $aResult, $iRows, $iColumns, $iRval
  348.     If $sCallBack <> "" Then
  349.         $aResult = "SQLITE_CALLBACK:" & $sCallBack
  350.         $iRval = _SQLite_GetTable2d($hDB, $sSQL, $aResult, $iRows, $iColumns)
  351.         If @error Then Return SetError(3, 0, $iRval)
  352.         Return $iRval
  353.     EndIf
  354.     $avRval = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_exec", _
  355.             "ptr", $hDB, _ ; An open database
  356.             "str", $sSQL, _ ; SQL to be executed
  357.             "ptr", 0, _ ; Callback function
  358.             "ptr", 0, _ ; 1st argument to callback function
  359.             "long_ptr", 0); Error msg written here
  360.     If @error > 0 Then
  361.         Return SetError(1, 0, $SQLITE_MISUSE) ; DllCall Error
  362.     EndIf
  363.     If $avRval[0] <> $SQLITE_OK Then
  364.         __SQLite_ReportError($hDB, "_SQLite_Exec", $sSQL)
  365.         SetError(-1)
  366.     EndIf
  367.     If $avRval[5] <> 0 Then DllCall($g_hDll_SQLite, "none:cdecl", "sqlite3_free", "ptr", $avRval[5])
  368.     Return $avRval[0]
  369. EndFunc   ;==>_SQLite_Exec
  370.  
  371. ;===============================================================================
  372. ;
  373. ; Function Name:    _SQLite_LibVersion()
  374. ; Description:      Returns the version number of the library
  375. ; Return Value(s):  Returns version number
  376. ; @error Value(s):    1 - Error Calling SQLite API 'sqlite3_libversion'
  377. ; Author(s):        piccaso (Fida Florian)
  378. ;
  379. ;===============================================================================
  380. ;
  381. Func _SQLite_LibVersion()
  382.     Local $r = DllCall($g_hDll_SQLite, "str:cdecl", "sqlite3_libversion")
  383.     If @error > 0 Then
  384.         Return SetError(1, 0, 0); DLLCall Error
  385.     Else
  386.         Return $r[0]
  387.     EndIf
  388. EndFunc   ;==>_SQLite_LibVersion
  389.  
  390. ;===============================================================================
  391. ;
  392. ; Function Name:    _SQLite_LastInsertRowID()
  393. ; Description:      Returns the ROWID of the most recent insert in the database
  394. ; Parameter(s):     $hDB - Optional, An Open Database, Default is the Last Opened Database
  395. ; Return Value(s):  Returns ROWID
  396. ; @error Value(s):    1 - Error Calling SQLite API 'sqlite3_last_insert_rowid'
  397. ;                     2 - Call Prevented by SaveMode
  398. ; Author(s):        piccaso (Fida Florian)
  399. ;
  400. ;===============================================================================
  401. ;
  402. Func _SQLite_LastInsertRowID($hDB = -1)
  403.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(2, 0, 0)
  404.     Local $r = DllCall($g_hDll_SQLite, "long:cdecl", "sqlite3_last_insert_rowid", "ptr", $hDB)
  405.     If @error > 0 Then
  406.         Return SetError(1, 0, 0) ;DllCall Error
  407.     Else
  408.         Return $r[0]
  409.     EndIf
  410. EndFunc   ;==>_SQLite_LastInsertRowID
  411.  
  412. ;===============================================================================
  413. ;
  414. ; Function Name:    _SQLite_Changes()
  415. ; Description:      Returns the number of database rows that were changed by the most recently completed query
  416. ; Parameter(s):     $hDB - Optional, An Open Database, Default is the Last Opened Database
  417. ; Return Value(s):  Returns number of Changes
  418. ; @error Value(s):    1 - Error Calling SQLite API 'sqlite3_changes'
  419. ;                     2 - Call Prevented by SaveMode
  420. ; Author(s):        piccaso (Fida Florian)
  421. ;
  422. ;===============================================================================
  423. ;
  424. Func _SQLite_Changes($hDB = -1)
  425.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(2, 0, 0)
  426.     Local $r = DllCall($g_hDll_SQLite, "long:cdecl", "sqlite3_changes", "ptr", $hDB)
  427.     If @error > 0 Then
  428.         Return SetError(1, 0, 0) ;DllCall Error
  429.     Else
  430.         Return $r[0]
  431.     EndIf
  432. EndFunc   ;==>_SQLite_Changes
  433.  
  434. ;===============================================================================
  435. ;
  436. ; Function Name:    _SQLite_TotalChanges()
  437. ; Description:      Returns the total number of database rows that have be modified, inserted, or deleted since the database connection was created
  438. ; Parameter(s):     $hDB - Optional, An Open Database, Default is the Last Opened Database
  439. ; Return Value(s):  Returns number of Total Changes
  440. ; @error Value(s):    1 - Error Calling SQLite API 'sqlite3_total_changes'
  441. ;                     2 - Call Prevented by SaveMode
  442. ; Author(s):        piccaso (Fida Florian)
  443. ;
  444. ;===============================================================================
  445. ;
  446. Func _SQLite_TotalChanges($hDB = -1)
  447.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(2, 0, 0)
  448.     Local $r = DllCall($g_hDll_SQLite, "long:cdecl", "sqlite3_total_changes", "ptr", $hDB)
  449.     If @error > 0 Then
  450.         Return SetError(1, 0, 0) ;DllCall Error
  451.     Else
  452.         Return $r[0]
  453.     EndIf
  454. EndFunc   ;==>_SQLite_TotalChanges
  455.  
  456. ;===============================================================================
  457. ;
  458. ; Function Name:    _SQLite_ErrCode()
  459. ; Description:      Returns the error code for the most recent failed sqlite3_* API call
  460. ; Parameter(s):     $hDB - Optional, An Open Database, Default is the Last Opened Database
  461. ; Return Value(s):  On Success - Return Value can be compared against $SQLITE_* Constants
  462. ; @error Value(s):    1 - Error Calling SQLite API 'sqlite3_errcode'
  463. ; Author(s):        piccaso (Fida Florian)
  464. ;
  465. ;===============================================================================
  466. ;
  467. Func _SQLite_ErrCode($hDB = -1)
  468.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return $SQLITE_MISUSE
  469.     Local $r = DllCall($g_hDll_SQLite, "long:cdecl", "sqlite3_errcode", "ptr", $hDB)
  470.     If @error > 0 Then
  471.         Return SetError(1, 0, $SQLITE_MISUSE) ;DllCall Error
  472.     Else
  473.         Return $r[0]
  474.     EndIf
  475. EndFunc   ;==>_SQLite_ErrCode
  476.  
  477. ;===============================================================================
  478. ;
  479. ; Function Name:    _SQLite_ErrMsg()
  480. ; Description:      Returns a String describing in English the error condition for the most recent sqlite3_* API call
  481. ; Parameter(s):     $hDB - Optional, An Open Database, Default is the Last Opened Database
  482. ; Return Value(s):  On Success - Returns Error message
  483. ; @error Value(s):    1 - Error Calling SQLite API 'sqlite3_errmsg'
  484. ; Author(s):        piccaso (Fida Florian)
  485. ;
  486. ;===============================================================================
  487. ;
  488. Func _SQLite_ErrMsg($hDB = -1)
  489.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return "Library used incorrectly"
  490.     Local $r = DllCall($g_hDll_SQLite, "str:cdecl", "sqlite3_errmsg", "ptr", $hDB)
  491.     If @error > 0 Then
  492.         Return SetError(1, 0, "Library used incorrectly") ;DllCall Error
  493.     Else
  494.         Return $r[0]
  495.     EndIf
  496. EndFunc   ;==>_SQLite_ErrMsg
  497.  
  498. ;===============================================================================
  499. ;
  500. ; Function Name:    _SQLite_Display2DResult()
  501. ; Description:      Prints a 2Dimensional Array formated to Console
  502. ; Parameter(s):     $aResult - The Array to be displayed
  503. ;                    $iCellWidth - Optional, Specifies the size of a Data Field
  504. ;                    $fReturn - Optional, If true The Formated String is returned
  505. ; Return Value(s):  none or Formated String
  506. ; @error Value(s):    1 - $aResult is no Array or has wrong Dimension
  507. ; Author(s):        piccaso (Fida Florian)
  508. ;
  509. ;===============================================================================
  510. ;
  511. Func _SQLite_Display2DResult($aResult, $iCellWidth = 0, $fReturn = False)
  512.     Local $iCol, $iRow, $sOut, $aiCellWidth, $iCellWidthUsed
  513.     If Not IsArray($aResult) Or UBound($aResult, 0) <> 2 Then
  514.         Return SetError(1, 0, "")
  515.     EndIf
  516.     If $iCellWidth = 0 Or IsKeyword($iCellWidth) Then
  517.         Dim $aiCellWidth[UBound($aResult, 2) ]
  518.         For $iCol = 0 To UBound($aResult, 1) - 1
  519.             For $iRow = 0 To UBound($aResult, 2) - 1
  520.                 If StringLen($aResult[$iCol][$iRow]) > $aiCellWidth[$iRow] Then
  521.                     $aiCellWidth[$iRow] = StringLen($aResult[$iCol][$iRow])
  522.                 EndIf
  523.             Next
  524.         Next
  525.     EndIf
  526.     For $iCol = 0 To UBound($aResult, 1) - 1
  527.         For $iRow = 0 To UBound($aResult, 2) - 1
  528.             If $iCellWidth = 0 Then
  529.                 $iCellWidthUsed = $aiCellWidth[$iRow]
  530.             Else
  531.                 $iCellWidthUsed = $iCellWidth
  532.             EndIf
  533.             If $fReturn Then
  534.                 $sOut &= StringFormat(" %-" & $iCellWidthUsed & "s ", $aResult[$iCol][$iRow])
  535.             Else
  536.                 ConsoleWrite(StringFormat(" %-" & $iCellWidthUsed & "s ", $aResult[$iCol][$iRow]))
  537.             EndIf
  538.         Next
  539.         If $fReturn Then
  540.             $sOut &= @CRLF
  541.         Else
  542.             ConsoleWrite(@CR)
  543.         EndIf
  544.     Next
  545.     If $fReturn Then Return $sOut
  546. EndFunc   ;==>_SQLite_Display2DResult
  547.  
  548. ;===============================================================================
  549. ;
  550. ; Function Name:    _SQLite_GetTable2d()
  551. ; Description:      Passes Out a 2Dimensional Array Containing Tablenames and Data of Executed Query
  552. ; Parameter(s):     $hDB - An Open Database, Use -1 To use Last Opened Database
  553. ;                    $sSQL - SQL Statement to be executed
  554. ;                    ByRef $aResult - Passes out the Result
  555. ;                    ByRef $iRows - Passes out the amount of 'data' Rows
  556. ;                    ByRef $iColumns - Passes out the amount of Columns
  557. ;                    $iCharSize - Optional, Specifies the maximal size of a Data Field
  558. ;                    $fSwichDimensions - Optional, Swiches Dimensions
  559. ; Return Value(s):  On Success - Returns $SQLITE_OK
  560. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  561. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  562. ;                     1 - Error Calling SQLite API 'sqlite3_get_table'
  563. ;                     2 - Error Calling SQLite API 'sqlite3_free_table'
  564. ;                     3 - Call Prevented by SaveMode
  565. ; Author(s):        piccaso (Fida Florian), blink314
  566. ;
  567. ;===============================================================================
  568. ;
  569. Func _SQLite_GetTable2d($hDB, $sSQL, ByRef $aResult, ByRef $iRows, ByRef $iColumns, $iCharSize = -1, $fSwichDimensions = False)
  570.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(3, 0, $SQLITE_MISUSE)
  571.     Local $r, $iResultSize, $i, $struct1, $pResult, $struct2, $iColCnt, $iRowCnt, $sCallBack, $iCbRval
  572.     If $iCharSize = 0 Or $iCharSize = "" Or $iCharSize < 1 Or IsKeyword($iCharSize) Then $iCharSize = -1
  573.     If IsKeyword($fSwichDimensions) Then $fSwichDimensions = False
  574.     $r = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_get_table", _
  575.             "ptr", $hDB, _ ; An open database
  576.             "str", $sSQL, _; SQL to be executed
  577.             "long_ptr", 0, _ ; Result written to a char *[]  that this points to
  578.             "long_ptr", 0, _ ; Number of result rows written here
  579.             "long_ptr", 0, _ ; Number of result columns written here
  580.             "long_ptr", 0)  ; Error msg written here
  581.     If @error > 0 Then
  582.         Return SetError(1, 0, $SQLITE_MISUSE) ; Dll Calling Error
  583.     EndIf
  584.     $pResult = $r[3]
  585.     $iRows = $r[4]
  586.     $iColumns = $r[5]
  587.     If $iColumns > 0 And $iRows > 0 Then
  588.         $iResultSize = (($iRows) + 1) * ($iColumns)
  589.         For $i = 1 To $iResultSize - 1
  590.             $struct1 &= "ptr;"
  591.         Next
  592.         $struct1 &= "ptr"
  593.         $struct2 = DllStructCreate($struct1, $pResult)
  594.         If Not IsArray($aResult) Then
  595.             If StringLeft($aResult, 16) = "SQLITE_CALLBACK:" Then $sCallBack = StringTrimLeft($aResult, 16)
  596.             Dim $aResult[1][1]
  597.         EndIf
  598.         If $sCallBack <> "" Then
  599.             ReDim $aResult [$iColumns]
  600.             For $i = 1 To $iResultSize
  601.                 $aResult[$iColCnt] = __SQLite_szStringRead(DllStructGetData($struct2, $i), $iCharSize)
  602.                 $iColCnt += 1
  603.                 If ($i / $iColumns) = Round($i / $iColumns, 0) Then
  604.                     $iCbRval = Call($sCallBack, $aResult)
  605.                     If $iCbRval = $SQLITE_ABORT Or $iCbRval = $SQLITE_INTERRUPT Or @error Then
  606.                         DllCall($g_hDll_SQLite, "none:cdecl", "sqlite3_free_table", "ptr", $pResult) ; pointer to 'resultp' from sqlite3_get_table
  607.                         Return SetError(99, 0, $r[0]) ; Internal only
  608.                     EndIf
  609.                     $iColCnt = 0
  610.                 EndIf
  611.             Next
  612.         Else
  613.             If Not $fSwichDimensions Then
  614.                 ReDim $aResult [$iRows + 1][$iColumns]
  615.                 For $i = 1 To $iResultSize
  616.                     $aResult[$iRowCnt][$iColCnt] = __SQLite_szStringRead(DllStructGetData($struct2, $i), $iCharSize)
  617.                     $iColCnt += 1
  618.                     If ($i / $iColumns) = Round($i / $iColumns, 0) Then
  619.                         $iRowCnt += 1
  620.                         $iColCnt = 0
  621.                     EndIf
  622.                 Next
  623.             Else
  624.                 ReDim $aResult [$iColumns][$iRows + 1]
  625.                 For $i = 1 To $iResultSize
  626.                     $aResult[$iColCnt][$iRowCnt] = __SQLite_szStringRead(DllStructGetData($struct2, $i), $iCharSize)
  627.                     $iColCnt += 1
  628.                     If ($i / $iColumns) = Round($i / $iColumns, 0) Then
  629.                         $iRowCnt += 1
  630.                         $iColCnt = 0
  631.                     EndIf
  632.                 Next
  633.             EndIf
  634.         EndIf
  635.     EndIf
  636.     DllCall($g_hDll_SQLite, "none:cdecl", "sqlite3_free_table", "ptr", $pResult) ; pointer to 'resultp' from sqlite3_get_table
  637.     If @error > 0 Then SetError(2)
  638.     If $r[0] <> $SQLITE_OK Then
  639.         __SQLite_ReportError($hDB, "_SQLite_GetTable2d or _SQLite_QuerySingleRow or _SQLite_Exec", $sSQL)
  640.         SetError(-1)
  641.     EndIf
  642.     Return $r[0]
  643. EndFunc   ;==>_SQLite_GetTable2d
  644.  
  645. ;===============================================================================
  646. ;
  647. ; Function Name:    _SQLite_SetTimeout()
  648. ; Description:      Sets Timeout for busy handler
  649. ; Parameter(s):     $hDB - An Open Database, Use -1 To use Last Opened Database
  650. ;                   $iTimeout - Timeout [msec]
  651. ; Return Value(s):  On Success - Returns $SQLITE_OK
  652. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  653. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  654. ;                     1 - Error Calling SQLite API 'sqlite3_busy_timeout'
  655. ;                     2 - Call prevented by SaveMode
  656. ; Author(s):        piccaso (Fida Florian)
  657. ;
  658. ;===============================================================================
  659. ;
  660. Func _SQLite_SetTimeout($hDB = -1, $iTimeout = 1000)
  661.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(2, 0, $SQLITE_MISUSE)
  662.     Local $avRval
  663.     If IsKeyword($iTimeout) Then $iTimeout = 1000
  664.     $avRval = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_busy_timeout", "ptr", $hDB, "int", $iTimeout)
  665.     If @error > 0 Then
  666.         SetError(1); DLLCall Error
  667.     ElseIf $avRval[0] <> $SQLITE_OK Then
  668.         SetError(-1)
  669.     EndIf
  670.     Return $avRval[0]
  671. EndFunc   ;==>_SQLite_SetTimeout
  672.  
  673.  
  674. ;===============================================================================
  675. ;
  676. ; Function Name:    _SQlite_Query()
  677. ; Description:      Prepares a SQLite Query
  678. ; Parameter(s):     $hDB - An Open Database, Use -1 To use Last Opened Database
  679. ;                    $sSQL - SQL Statement to be executed
  680. ;                    ByRef $hQuery - Passes out a Query Handle
  681. ; Return Value(s):  On Success - Returns $SQLITE_OK
  682. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  683. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  684. ;                     1 - Error Calling SQLite API 'sqlite3_prepare_v2'
  685. ;                     2 - Call prevented by SafeMode
  686. ; Author(s):        piccaso (Fida Florian)
  687. ;
  688. ;===============================================================================
  689. ;
  690. Func _SQlite_Query($hDB, $sSQL, ByRef $hQuery)
  691.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(2, 0, $SQLITE_MISUSE)
  692.     Local $iRval
  693.     $iRval = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_prepare_v2", _
  694.             "ptr", $hDB, _
  695.             "str", $sSQL, _
  696.             "int", StringLen($sSQL), _
  697.             "long_ptr", 0, _
  698.             "long_ptr", 0)
  699.     If @error > 0 Then
  700.         Return SetError(1, 0, $SQLITE_MISUSE)
  701.     ElseIf $iRval[0] = $SQLITE_OK Then
  702.         $hQuery = $iRval[4]
  703.         __SQLite_hAdd($iRval[4], $SQLITE_QUERYHANDLE)
  704.         Return $iRval[0]
  705.     Else
  706.         __SQLite_ReportError($hDB, "_SQLite_Query", $sSQL)
  707.         Return SetError(-1, 0, $iRval[0])
  708.     EndIf
  709. EndFunc   ;==>_SQlite_Query
  710.  
  711. ;===============================================================================
  712. ;
  713. ; Function Name:    _SQLite_FetchData()
  714. ; Description:      Fetches 1 Row of Data from a _SQLite_Query() based query
  715. ; Parameter(s):     $hQuery - Queryhandle passed out by _SQLite_Query()
  716. ;                    ByRef $aRow - A 1 dimensional Array containing a Row of Data
  717. ;                    $fBinary - Switch for Binary mode ($aRow will be a Array of Binary Strings)
  718. ; Return Value(s):  On Success - Returns $SQLITE_OK
  719. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  720. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  721. ;                     1 - Error Calling SQLite API 'sqlite3_step'
  722. ;                     2 - Error Calling SQLite API 'sqlite3_data_count'
  723. ;                     3 - Error Calling SQLite API 'sqlite3_column_text'
  724. ;                     4 - Error Calling SQLite API 'sqlite3_column_type'
  725. ;                     5 - Error Calling SQLite API 'sqlite3_column_bytes'
  726. ;                     6 - Error Calling SQLite API 'sqlite3_column_blob'
  727. ;                     7 - Call prevented by SaveMode
  728. ; Author(s):        piccaso (Fida Florian)
  729. ;
  730. ;===============================================================================
  731. ;
  732. Func _SQLite_FetchData($hQuery, ByRef $aRow, $fBinary = False)
  733.     If Not __SQLite_hChk($hQuery, $SQLITE_QUERYHANDLE) = $SQLITE_OK Then Return SetError(7, 0, $SQLITE_MISUSE)
  734.     If Not IsArray($aRow) Then
  735.         Dim $aRow[1]
  736.     EndIf
  737.     Local $iRval_Step, $iRval_ColCnt, $sRval, $i, $iRval_coltype
  738.     Local $iColBytes, $vResult, $vResultStruct
  739.     Local $SQLITE_NULL = 5
  740.     If IsKeyword($fBinary) Then $fBinary = False
  741.     $iRval_Step = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_step", "ptr", $hQuery)
  742.     If @error > 0 Then
  743.         Return SetError(1, 0, $SQLITE_MISUSE)
  744.     ElseIf $iRval_Step[0] = $SQLITE_ROW Then
  745.         $iRval_ColCnt = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_data_count", "ptr", $hQuery)
  746.         If @error > 0 Then
  747.             Return SetError(2, 0, $SQLITE_MISUSE)
  748.         EndIf
  749.         If $iRval_ColCnt[0] > 0 Then
  750.             ReDim $aRow[$iRval_ColCnt[0]]
  751.             For $i = 0 To $iRval_ColCnt[0] - 1
  752.                 If Not $fBinary Then
  753.                     $iRval_coltype = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_column_type", "ptr", $hQuery, "int", $i)
  754.                     If @error > 0 Then
  755.                         Return SetError(4, 0, $SQLITE_MISUSE)
  756.                     EndIf
  757.                     If $iRval_coltype[0] = $SQLITE_NULL Then
  758.                         $aRow[$i] = ""
  759.                         ContinueLoop
  760.                     EndIf
  761.                     $sRval = DllCall($g_hDll_SQLite, "str:cdecl", "sqlite3_column_text", "ptr", $hQuery, "int", $i)
  762.                     If @error > 0 Then
  763.                         Return SetError(3, 0, $SQLITE_MISUSE)
  764.                     EndIf
  765.                     $aRow[$i] = $sRval[0]
  766.                 Else
  767.                     $iColBytes = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_column_bytes", "ptr", $hQuery, "int", $i)
  768.                     If @error > 0 Then
  769.                         Return SetError(5, 0, $SQLITE_MISUSE)
  770.                     EndIf
  771.                     $vResult = DllCall($g_hDll_SQLite, "ptr:cdecl", "sqlite3_column_blob", "ptr", $hQuery, "int", $i)
  772.                     If @error > 0 Then
  773.                         Return SetError(6, 0, $SQLITE_MISUSE)
  774.                     EndIf
  775.                     $vResultStruct = DllStructCreate("byte[" & $iColBytes[0] & "]", $vResult[0])
  776.                     $aRow[$i] = Binary(DllStructGetData($vResultStruct, 1))
  777.                 EndIf
  778.             Next
  779.             Return $SQLITE_OK
  780.         Else
  781.             Return SetError(-1, 0, $SQLITE_EMPTY)
  782.         EndIf
  783.     Else ; incl. $SQLITE_DONE
  784.         _SQLite_QueryFinalize($hQuery)
  785.         If $iRval_Step[0] <> $SQLITE_OK Then SetError(-1)
  786.         Return $iRval_Step[0]
  787.     EndIf
  788. EndFunc   ;==>_SQLite_FetchData
  789.  
  790. ;===============================================================================
  791. ;
  792. ; Function Name:    _SQLite_Close()
  793. ; Description:      Closes a open Database, Waits until SQLite <> $SQLITE_BUSY until 'global Timeout' has elapsed
  794. ; Parameter(s):     $hDB - Optional Database Handle
  795. ; Return Value(s):  On Success - Returns $SQLITE_OK
  796. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  797. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  798. ;                     1 - Error Calling SQLite API 'sqlite3_close'
  799. ;                     2 - Call prevented by SaveMode
  800. ; Author(s):        piccaso (Fida Florian)
  801. ;
  802. ;===============================================================================
  803. ;
  804. Func _SQLite_Close($hDB = -1) ; Closes Database
  805.     If Not __SQLite_hChk($hDB, $SQLITE_DBHANDLE) = $SQLITE_OK Then Return SetError(2, 0, $SQLITE_MISUSE)
  806.     Local $iRval
  807.     $iRval = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_close", "ptr", $hDB) ; An open database
  808.     If @error > 0 Then
  809.         Return SetError(1, 0, $SQLITE_MISUSE) ; Dll Calling Error
  810.     ElseIf $iRval[0] = $SQLITE_OK Then
  811.         $g_hDB_SQLite = 0
  812.         __SQLite_hDel($hDB, $SQLITE_DBHANDLE)
  813.         Return $iRval[0]
  814.     EndIf
  815.     __SQLite_ReportError($hDB, "_SQLite_Close")
  816.     Return SetError(-1, 0, $iRval[0])
  817. EndFunc   ;==>_SQLite_Close
  818.  
  819. ;===============================================================================
  820. ;
  821. ; Function Name:    _SQLite_SaveMode()
  822. ; Description:      Disable or Enable Save Mode
  823. ; Parameter(s):     $fSaveModeState    - True or False to enable or disable SafeMode
  824. ; Return Value(s):  On Success - Returns $SQLITE_OK
  825. ;                   On Failure - Returns $SQLITE_MISUSE
  826. ; @error Value(s):    1 - Error Interpreting $fSaveModeState Parameter
  827. ; Author(s):        piccaso (Fida Florian)
  828. ;
  829. ;===============================================================================
  830. ;
  831. Func _SQLite_SaveMode($fSaveModeState) ; Turn Savemode On or Off (boolean)
  832.     If $fSaveModeState = False Then
  833.         $g_avSafeMode_SQLite[0] = False
  834.     ElseIf $fSaveModeState = True Then
  835.         $g_avSafeMode_SQLite[0] = True
  836.     Else
  837.         Return SetError(1, 0, $SQLITE_MISUSE)
  838.     EndIf
  839.     Return $SQLITE_OK
  840. EndFunc   ;==>_SQLite_SaveMode
  841.  
  842. ;===============================================================================
  843. ;
  844. ; Function Name:    _SQLite_QueryFinalize()
  845. ; Description:      Finalize _SQLite_Query() based query
  846. ; Parameter(s):     $hQuery    - Query Handle Generated by SQLite_Query()
  847. ; Return Value(s):  On Success - Returns $SQLITE_OK
  848. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  849. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  850. ;                     1 - Error Calling SQLite API 'sqlite3_finalize'
  851. ;                     2 - Call prevented by SaveMode
  852. ; Author(s):        piccaso (Fida Florian)
  853. ;
  854. ;===============================================================================
  855. ;
  856. Func _SQLite_QueryFinalize($hQuery)
  857.     If Not __SQLite_hChk($hQuery, $SQLITE_QUERYHANDLE) = $SQLITE_OK Then Return SetError(2, 0, $SQLITE_MISUSE)
  858.     Local $avRval = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_finalize", "ptr", $hQuery)
  859.     If @error > 0 Then
  860.         Return SetError(1, 0, $SQLITE_MISUSE)
  861.     EndIf
  862.     __SQLite_hDel($hQuery, $SQLITE_QUERYHANDLE)
  863.     If $avRval[0] <> $SQLITE_OK Then SetError(-1)
  864.     Return $avRval[0]
  865. EndFunc   ;==>_SQLite_QueryFinalize
  866.  
  867. ;===============================================================================
  868. ;
  869. ; Function Name:    _SQlite_QueryReset()
  870. ; Description:      Reset a _SQLite_Query() based query
  871. ; Parameter(s):     $hQuery    - Query Handle Generated by SQLite_Query()
  872. ; Return Value(s):  On Success - Returns $SQLITE_OK
  873. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  874. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  875. ;                     1 - Error Calling SQLite API 'sqlite3_reset'
  876. ;                     2 - Call prevented by SaveMode
  877. ; Author(s):        piccaso (Fida Florian)
  878. ;
  879. ;===============================================================================
  880. ;
  881. Func _SQlite_QueryReset($hQuery)
  882.     If Not __SQLite_hChk($hQuery, $SQLITE_QUERYHANDLE) = $SQLITE_OK Then Return SetError(2, 0, $SQLITE_MISUSE)
  883.     Local $avRval = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_reset", "ptr", $hQuery)
  884.     If @error > 0 Then
  885.         Return SetError(1, 0, $SQLITE_MISUSE)
  886.     EndIf
  887.     If $avRval[0] <> $SQLITE_OK Then SetError(-1)
  888.     Return $avRval[0]
  889. EndFunc   ;==>_SQlite_QueryReset
  890.  
  891. ;===============================================================================
  892. ;
  893. ; Function Name:    _SQLite_FetchNames()
  894. ; Description:      Read out the Tablenames of a _SQLite_Query() based query
  895. ; Parameter(s):     $hQuery    - Query Handle Generated by SQLite_Query()
  896. ;                   ByRef $aNames - 1 Dimensional Array Containign the Table Names
  897. ; Return Value(s):  On Success - Returns $SQLITE_OK
  898. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  899. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  900. ;                     1 - Error Calling SQLite API 'sqlite3_data_count'
  901. ;                     2 - Error Calling SQLite API 'sqlite3_column_name'
  902. ;                     3 - Call prevented by SaveMode
  903. ; Author(s):        piccaso (Fida Florian)
  904. ;
  905. ;===============================================================================
  906. ;
  907. Func _SQLite_FetchNames($hQuery, ByRef $aNames)
  908.     If Not __SQLite_hChk($hQuery, $SQLITE_QUERYHANDLE) = $SQLITE_OK Then Return SetError(3, 0, $SQLITE_MISUSE)
  909.     Local $avDataCnt, $avColName, $iCnt
  910.     If Not IsArray($aNames) Then Dim $aNames[1]
  911.     $avDataCnt = DllCall($g_hDll_SQLite, "int:cdecl", "sqlite3_column_count", "ptr", $hQuery)
  912.     If @error > 0 Then
  913.         Return SetError(1, 0, $SQLITE_MISUSE) ; DllCall Error (sqlite3_data_count)
  914.     ElseIf $avDataCnt[0] > 0 Then
  915.         ReDim $aNames[$avDataCnt[0]]
  916.         For $iCnt = 0 To $avDataCnt[0] - 1
  917.             $avColName = DllCall($g_hDll_SQLite, "str:cdecl", "sqlite3_column_name", "ptr", $hQuery, "int", $iCnt)
  918.             If @error > 0 Then
  919.                 Return SetError(2, 0, $SQLITE_MISUSE); DllCall Error (sqlite3_column_name)
  920.             EndIf
  921.             $aNames[$iCnt] = $avColName[0]
  922.         Next
  923.         Return $SQLITE_OK
  924.     Else
  925.         Return SetError(-1, 0, $SQLITE_EMPTY)
  926.     EndIf
  927. EndFunc   ;==>_SQLite_FetchNames
  928.  
  929. ;===============================================================================
  930. ;
  931. ; Function Name:    _SQLite_QuerySingleRow()
  932. ; Description:      Read out the first Row of the Result from the Specified query
  933. ; Parameter(s):     $hDB - An Open Database, Use -1 To use Last Opened Database
  934. ;                    $sSQL - SQL Statement to be executed
  935. ; Return Value(s):  On Success - Returns $SQLITE_OK
  936. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  937. ; @error Value(s):    -1 - SQLite Reported an Error (Check Return value)
  938. ;                     1 - Error Calling SQLite API 'sqlite3_get_table'
  939. ;                     2 - Error Calling SQLite API 'sqlite3_free_table'
  940. ;                     3 - Call Prevented by SaveMode
  941. ; Author(s):        piccaso (Fida Florian)
  942. ;
  943. ;===============================================================================
  944. ;
  945. Func _SQLite_QuerySingleRow($hDB, $sSQL, ByRef $aRow)
  946.     Local $aResult, $iRows, $iColumns, $iRval, $i, $iAu3Error
  947.     $aRow = ""
  948.     Dim $aRow[1]
  949.     $iRval = _SQLite_GetTable2d($hDB, $sSQL, $aResult, $iRows, $iColumns)
  950.     $iAu3Error = @error
  951.     If $iRval = $SQLITE_OK And UBound($aResult, 0) > 0 Then
  952.         ReDim $aRow[UBound($aResult, 2) ]
  953.         For $i = 0 To UBound($aResult, 2) - 1
  954.             $aRow[$i] = $aResult[1][$i]
  955.         Next
  956.     EndIf
  957.     If $iAu3Error Then SetError($iAu3Error)
  958.     Return $iRval
  959. EndFunc   ;==>_SQLite_QuerySingleRow
  960.  
  961. ;===============================================================================
  962. ;
  963. ; Function Name:    _SQLite_SQLiteExe()
  964. ; Description:      Executes commands in SQLite.exe
  965. ; Parameter(s):     $sDatabaseFile - Database Filename
  966. ;                    $sInput - Commands for SQLite.exe
  967. ;                    $sOutput - Raw Output from SQLite.exe
  968. ;                    $sSQLiteExeFilename - optional, Path to SQlite3.exe (-1 is default)
  969. ; Return Value(s):  On Success - Returns $SQLITE_OK
  970. ;                   On Failure - Return Value can be compared against $SQLITE_* Constants
  971. ; @error Value(s):    1 - Cant create new Database
  972. ;                    2 - SQLite3.exe not Found
  973. ;                    3 - SQL error / Incomplete SQL
  974. ;                    4 - Cant open input file
  975. ; Author(s):        piccaso (Fida Florian)
  976. ;
  977. ;===============================================================================
  978. ;
  979. Func _SQLite_SQLiteExe($sDatabaseFile, $sInput, ByRef $sOutput, $sSQLiteExeFilename = -1, $fDebug = False)
  980.     Local $sInputFile = _TempFile(), $sOutputFile = _TempFile(), $iRval = $SQLITE_OK, $nErrorLevel
  981.     Local $nRunErrorsFatalOld = Opt("RunErrorsFatal", 0), $hInputFile, $sCmd, $hNewFile
  982.     If $sSQLiteExeFilename = -1 Or IsKeyword($sSQLiteExeFilename) Then $sSQLiteExeFilename = "SQLite3.exe"
  983.     If Not FileExists($sDatabaseFile) Then
  984.         $hNewFile = FileOpen($sDatabaseFile, 2 + 8)
  985.         If $hNewFile = -1 Then
  986.             Return SetError(1, 0, $SQLITE_CANTOPEN) ; Cant Create new Database
  987.         EndIf
  988.         FileClose($hNewFile)
  989.     EndIf
  990.     $hInputFile = FileOpen($sInputFile, 2)
  991.     If $hInputFile > -1 Then
  992.         $sInput = ".output stdout" & @CRLF & $sInput
  993.         FileWrite($hInputFile, $sInput)
  994.         FileClose($hInputFile)
  995.         $sCmd = @ComSpec & " /c " & FileGetShortName($sSQLiteExeFilename) & '  "' _
  996.                  & FileGetShortName($sDatabaseFile) _
  997.                  & '" > "' & FileGetShortName($sOutputFile) _
  998.                  & '" < "' & FileGetShortName($sInputFile) & '"'
  999.         $nErrorLevel = RunWait($sCmd, @WorkingDir, @SW_HIDE)
  1000.         If $fDebug = True Then
  1001.             ConsoleWrite('@@ Debug(_SQLite_SQLiteExe) : $sCmd = ' & $sCmd & @LF & '>ErrorLevel: ' & $nErrorLevel & @LF)
  1002.         EndIf
  1003.         If @error = 1 Or $nErrorLevel = 1 Then
  1004.             $iRval = $SQLITE_MISUSE ; SQLite.exe not found
  1005.         Else
  1006.             $sOutput = FileRead($sOutputFile, FileGetSize($sOutputFile))
  1007.             If StringInStr($sOutput, "SQL error:", 1) > 0 Or StringInStr($sOutput, "Incomplete SQL:", 1) > 0 Then $iRval = $SQLITE_ERROR ; SQL error / Incomplete SQL
  1008.         EndIf
  1009.     Else
  1010.         $iRval = $SQLITE_CANTOPEN ; Cant open Input File
  1011.     EndIf
  1012.     Opt("RunErrorsFatal", $nRunErrorsFatalOld)
  1013.     If FileExists($sInputFile) Then FileDelete($sInputFile)
  1014.     Switch $iRval
  1015.         Case $SQLITE_MISUSE
  1016.             SetError(2)
  1017.         Case $SQLITE_ERROR
  1018.             SetError(3)
  1019.         Case $SQLITE_CANTOPEN
  1020.             SetError(4)
  1021.     EndSwitch
  1022.     Return $iRval
  1023. EndFunc   ;==>_SQLite_SQLiteExe
  1024.  
  1025. ;===============================================================================
  1026. ;
  1027. ; Function Name:    _SQLite_Encode()
  1028. ; Description:      Encodes Strings or Binary data for use in SQLite Query's
  1029. ; Parameter(s):     $vData - Data To be encoded (String, Number or BinaryString)
  1030. ; Return Value(s):  On Success - Returns Encoded String
  1031. ;                   On Failure - Returns Empty String
  1032. ; @error Value(s):    1 - Data could not be encoded
  1033. ; Author(s):        piccaso (Fida Florian)
  1034. ;
  1035. ;===============================================================================
  1036. ;
  1037. Func _SQLite_Encode($vData)
  1038.     Local $iCnt, $vRval
  1039.     If IsNumber($vData) Then $vData = String($vData)
  1040.     If IsString($vData) Or IsBinary($vData) Then
  1041.         $vRval = "X'"
  1042.         If StringLower(StringLeft($vData, 2)) = "0x" And Not IsBinary($vData) Then
  1043.             ; BinaryString would mess this up...
  1044.             For $iCnt = 1 To StringLen($vData)
  1045.                 $vRval &= Hex(Asc(StringMid($vData, $iCnt)), 2)
  1046.             Next
  1047.         Else
  1048.             ; BinaryString is Faster
  1049.             If Not IsBinary($vData) Then $vData = Binary($vData)
  1050.             $vRval &= Hex($vData)
  1051.         EndIf
  1052.         $vRval &= "'"
  1053.         Return $vRval
  1054.     EndIf
  1055.     Return SetError(1, 0, "")
  1056. EndFunc   ;==>_SQLite_Encode
  1057.  
  1058. ;===============================================================================
  1059. ;
  1060. ; Function Name:    _SQLite_Escape()
  1061. ; Description:      Escapes a String
  1062. ; Parameter(s):        $sString - String to escape.
  1063. ;                    $iBuffSize - Optional
  1064. ; Return Value(s):  On Success - Returns Escaped String
  1065. ;                   On Failure - Returns Empty String
  1066. ; @error Value(s):    1 - Error Calling SQLite API 'sqlite3_mprintf'
  1067. ; Author(s):        piccaso (Fida Florian)
  1068. ;
  1069. ;===============================================================================
  1070. ;
  1071. Func _SQLite_Escape($sString, $iBuffSize = Default)
  1072.     Local $aRval, $sResult
  1073.     $aRval = DllCall($g_hDll_SQLite, "ptr:cdecl", "sqlite3_mprintf", "str", "'%q'", "str", $sString)
  1074.     If @error > 0 Then Return SetError(1, 0, ""); DLLCall Error 'sqlite3_mprintf'
  1075.     If IsKeyword($iBuffSize) Or $iBuffSize < 1 Then $iBuffSize = -1
  1076.     $sResult = __SQLite_szStringRead($aRval[0], $iBuffSize)
  1077.     DllCall($g_hDll_SQLite, "none:cdecl", "sqlite3_free", "ptr", $aRval[0])
  1078.     Return $sResult
  1079. EndFunc   ;==>_SQLite_Escape
  1080.  
  1081. #region        SQLite.au3 Internal Functions
  1082. ; $g_avSafeMode_SQLite[0] -> Savemode State (boolean)
  1083. ; $g_avSafeMode_SQLite[1] -> Array containing known $hDB handles
  1084. ; $g_avSafeMode_SQLite[2] -> Array containing known $hQuery handles
  1085. ; $g_avSafeMode_SQLite[3] -> pseudo dll handle for 'msvcrt.dll'
  1086. ; $g_avSafeMode_SQLite[4] -> Array of Temp Files
  1087.  
  1088. Func __SQLite_hChk(ByRef $hGeneric, $iHandleType)
  1089.     If $hGeneric = -1 Or $hGeneric = "" Or IsKeyword($hGeneric) Then
  1090.         If $iHandleType = $SQLITE_DBHANDLE Then
  1091.             $hGeneric = $g_hDB_SQLite
  1092.         Else
  1093.             Return $SQLITE_ERROR
  1094.         EndIf
  1095.     EndIf
  1096.     If $g_avSafeMode_SQLite[0] = False Then
  1097.         Return $SQLITE_OK
  1098.     ElseIf _ArraySearch($g_avSafeMode_SQLite[$iHandleType], $hGeneric) > 0 Then
  1099.         Return $SQLITE_OK
  1100.     EndIf
  1101.     Return $SQLITE_ERROR
  1102. EndFunc   ;==>__SQLite_hChk
  1103.  
  1104. Func __SQLite_hAdd($hGeneric, $iHandleType)
  1105.     _ArrayAdd($g_avSafeMode_SQLite[$iHandleType], $hGeneric)
  1106. EndFunc   ;==>__SQLite_hAdd
  1107.  
  1108. Func __SQLite_hDel($hGeneric, $iHandleType)
  1109.     Local $iElement = _ArraySearch($g_avSafeMode_SQLite[$iHandleType], $hGeneric)
  1110.     If $iElement > 0 Then
  1111.         _ArrayDelete($g_avSafeMode_SQLite[$iHandleType], $iElement)
  1112.     EndIf
  1113. EndFunc   ;==>__SQLite_hDel
  1114.  
  1115. Func __SQLite_VersCmp($sFile, $sVersion)
  1116.     Local $avRval = DllCall($sFile, "int:cdecl", "sqlite3_libversion_number")
  1117.     If @error Then Return $SQLITE_CORRUPT ; Not SQLite3.dll or Not found
  1118.     If $avRval[0] >= $sVersion Then Return $SQLITE_OK ; Version OK
  1119.     Return $SQLITE_MISMATCH ; Version Older
  1120. EndFunc   ;==>__SQLite_VersCmp
  1121.  
  1122. Func __SQLite_hDbg()
  1123.     Local $i, $aTmp
  1124.     ConsoleWrite("State : " & $g_avSafeMode_SQLite[0] & @CR)
  1125.     $aTmp = $g_avSafeMode_SQLite[1]
  1126.     For $i = 0 To UBound($aTmp) - 1
  1127.         ConsoleWrite("$g_avSafeMode_SQLite[hDB]     -> [" & $i & "]" & $aTmp[$i] & @CR)
  1128.     Next
  1129.     $aTmp = $g_avSafeMode_SQLite[2]
  1130.     For $i = 0 To UBound($aTmp) - 1
  1131.         ConsoleWrite("$g_avSafeMode_SQLite[hQuery]  -> [" & $i & "]" & $aTmp[$i] & @CR)
  1132.     Next
  1133. EndFunc   ;==>__SQLite_hDbg
  1134.  
  1135. Func __SQLite_ReportError($hDB, $sFunction, $sQuery = Default, $sError = Default, $vReturnValue = Default)
  1136.     If @Compiled Then Return
  1137.     If IsKeyword($sError) Then $sError = _SQLite_ErrMsg($hDB)
  1138.     If IsKeyword($sQuery) Then $sQuery = ""
  1139.     Local $sOut
  1140.     $sOut &= "!   SQLite.au3 Error" & @LF
  1141.     $sOut &= "--> Function: " & $sFunction & @LF
  1142.     If $sQuery <> "" Then $sOut &= "--> Query:    " & $sQuery & @LF
  1143.     $sOut &= "--> Error:    " & $sError & @LF
  1144.     ConsoleWrite($sOut & @LF)
  1145.     If Not IsKeyword($vReturnValue) Then Return $vReturnValue
  1146. EndFunc   ;==>__SQLite_ReportError
  1147.  
  1148. Func __SQLite_szStringRead($iszPtr, $iLen = -1)
  1149.     Local $aStrLen, $vszString
  1150.     If $iszPtr < 1 Then Return ""
  1151.     If $iLen < 1 Then
  1152.         If $g_avSafeMode_SQLite[3] < 1 Then $g_avSafeMode_SQLite[3] = DllOpen("msvcrt.dll")
  1153.         $aStrLen = DllCall($g_avSafeMode_SQLite[3], "int:cdecl", "strlen", "ptr", $iszPtr)
  1154.         If @error Then Return SetError(1, 0, "")
  1155.         $iLen = $aStrLen[0] + 1
  1156.     EndIf
  1157.     $vszString = DllStructCreate("char[" & $iLen & "]", $iszPtr)
  1158.     If @error Then Return SetError(2, 0, "")
  1159.     Return SetError(0, $iLen, DllStructGetData($vszString, 1))
  1160. EndFunc   ;==>__SQLite_szStringRead
  1161. #endregion     SQLite.au3 Internal Functions